<template>
  <div v-if="show" @click="closeLogin" :class="real_them" :id="beforeid">
    <div v-if="ifbegin">
      <div class="tt">欢迎使用t_b博客，提供用户阅读博客，点赞评论，分享动态，修改个人信息等功能，同时适配移动端、PC端和不同窗口大小，黑夜模式和白昼模式。
      </div>
      <br/>
      <div class="tt">点击列表的任意文章(或点击悬浮窗)即可进行阅读使用。</div>
    </div>
    <div v-if="!ifbegin">
      <div class="article">
        <div class="headers">
          <h1 class="article-title">{{ articles.title }}</h1>
          <div class="tag">
            <TagVue
              v-for="(i, index) of tag"
              :key="index"
              :name="i[0]"
              :color="i[1]"
            />
          </div>
        </div>

        <div class="author-info-block">
          <div
            style="
              height: 50px;
              width: 50px;
              overflow: hidden;
              border-radius: 50%;
              margin-right: 10px;
            "
          >
            <img
              loading="eager"
              :src="articles.icon"
              style="height: 100%"
              alt="头像"
            />
          </div>

          <div class="author-info-box">
            <div class="author-name">
              <a
                href="/user/3263764828006760"
                target="_blank"
                rel=""
                class="username username ellipsis"
              >
                <span class="name" style="max-width: 128px">{{
                  articles.wirtername
                }}</span>
              </a>
            </div>

            <div class="meta-box">
              <time class="time">
                {{ time(articles.time) }}
              </time>
              <!-- <span class="views-count"> ·&nbsp;&nbsp;阅读 14268 </span> -->
            </div>
          </div>
          <!-- <button class="follow-button follow">
          <span class="text">关注</span>
        </button> -->
        </div>

        <div class="markdown-body">
          <div v-for="item in context" :key="item.message">
            <div class="textBody" v-if="item[0] == 'first_content'">
              <p style="margin: 15px 15px 15px 0px">{{ item[1] }}</p>
            </div>
            <div class="textBody codes" v-if="item[0] == 'code'">
              <div
                v-for="i in code[item[1]]"
                :key="i.message"
                style="display: flex"
              >
                <div v-for="x in (i[0] - 1) * 6" :key="x">&nbsp;</div>
                <pre
                  v-highlightjs
                  class="itemcode"
                ><code>{{ i[1] }}</code></pre>
              </div>
            </div>
            <div class="textBody" v-if="item[0] == 'main_title'">
              <h2 style="margin: 15px 15px 15px 0px">{{ item[1] }}</h2>
            </div>
            <div class="textBody" v-if="item[0] == 'mid_title'">
              <p style="margin: 15px 15px 15px 15px">{{ item[1] }}</p>
            </div>
            <div class="textBody" v-if="item[0] == 'sm_title'">
              <p style="margin: 15px 15px 15px 25px">{{ item[1] }}</p>
            </div>
            <div class="textBody" v-if="item[0] == 'second_content'">
              <p style="margin: 15px 15px 15px 25px">{{ item[1] }}</p>
            </div>
          </div>
        </div>

        <!-- 页面下方点赞 -->
        <div class="action-box action-bar">
          <div class="praise-action action">
            <div @click="addLike" :class="likeclass">
              <!-- action-title-box -->
              <i class="el-icon-thumb"></i>
              <span class="action-title">{{ articles.like }}</span>
            </div>
          </div>

          <div class="collect-action action">
            <div @click="collect" :class="collectclass">
              <i class="el-icon-star-off"></i>
              <span class="action-title">{{ articles.collect }}</span>
            </div>
          </div>

          <div class="comment-action action">
            <div @click="ifReply = !ifReply" :class="commentclass">
              <i class="el-icon-chat-dot-square"></i>
              <span class="action-title">{{
                ifReply
                  ? "取消回复"
                  : articles.comment
                  ? articles.comment.length
                  : 0
              }}</span>
            </div>
          </div>
        </div>
      </div>
      <div v-if="ifReply" class="reply">
        <el-input
          type="textarea"
          class="inner"
          placeholder="请输入内容"
          v-model="textarea"
          maxlength="100"
          show-word-limit
        >
        </el-input>
        <div @click="sendComment" class="btn">发布评论</div>
      </div>
      <div v-if="show">
        <commentVue
          v-for="i in articles.comments"
          :key="i.id"
          :allcomment="i"
        />
      </div>
    </div>
  </div>
</template>

<script>
import TagVue from "../re_components/Tag.vue";
import commentVue from "../re_components/comment.vue";
import LikeBlog from "../api/addLikeBlog.js";
import LikeComment from "../api/addCommentBlog.js";
import Collect from "../api/addCollect.js";
import { time } from "../utils/time";
export default {
  name: "Blog_main",
  components: { TagVue, commentVue },
  data() {
    return {
      allReadyLike: false,
      allReadycollect: false,
      ifReply: false,
      action: [],
      textarea: "",
      show: 1,
      ifbegin: true,
    };
  },
  methods: {
    time: time,
    collect() {
      if (!this.$store.state.islogin) {
        this.$message({
          message: "请先登录",
          type: "warning",
        });
        return;
      }
      console.log("@", this.$store.state.infomation);
      this.allReadycollect = !this.allReadycollect;
      let params = new URLSearchParams();
      params.append("blogid", this.$store.state.nowblog.id);
      params.append("userid", this.$store.state.infomation.id);
      Collect(params).then(() => {
        if (this.allReadycollect) {
          this.articles.collect++;
        } else {
          this.articles.collect--;
        }
        this.$message({
          message: "收藏成功，下次进入页面时显示最新数据",
          type: "success",
        });
      });
    },
    addLike() {
      // 博客的赞无法取消！
      if (!this.$store.state.islogin) {
        this.$message({
          message: "请先登录",
          type: "warning",
        });
        return;
      }
      this.allReadyLike = !this.allReadyLike;
      console.log(this.$store.state.nowblog, this.$store.state.nowblog.id);
      let params = new URLSearchParams();
      params.append("id", this.$store.state.nowblog.id);
      params.append("userid", this.$store.state.infomation.id);
      LikeBlog(params).then(() => {
        if (this.allReadyLike) {
          this.articles.like++;
        } else {
          this.articles.like--;
        }
        this.$message({
          message: "点赞成功，下次进入页面时显示最新数据",
          type: "success",
        });
        // console.log(res);
      });
    },
    closeLogin() {
      this.$store.commit("changeshow", false);
    },
    sendComment() {
      // 博客的赞无法取消！
      if (!this.$store.state.islogin) {
        this.$message({
          message: "请先登录",
          type: "warning",
        });
        return;
      }
      let params = new URLSearchParams();
      params.append("id", this.$store.state.nowblog.id);
      params.append("userid", this.$store.state.infomation.id);
      params.append("context", this.textarea);
      LikeComment(params).then((res) => {
        console.log(res);
        this.ifReply = false;
        this.$message({
          message: "成功回复，下次进入页面时显示最新数据",
          type: "success",
        });
      });
    },
  },
  mounted() {
    let t = setInterval(() => {
      if (this.$store.state.nowblog && this.$store.state.nowblog.id) {
        this.ifbegin = false;
        clearInterval(t)
      }
    }, 50);
    this.beforeid = this.$store.state.nowblog.id;
  },
  watch: {
    beforeid: {
      immediate: true,
      handler() {
        this.show = 0;
        this.$nextTick(() => {
          this.show = 1;
        });
      },
    },
    id: {
      handler() {
        this.allReadyLike = false;
        this.allReadycollect = false;
        this.ifReply = false;
        let arr = JSON.parse(this.$store.state.nowblog.likeusers);
        let collect = JSON.parse(
          JSON.parse(this.$store.state.infomation.collection)
        );
        if (arr !== null) {
          for (let i = 0; i < arr.length; i++) {
            if (arr[i] == this.$store.state.infomation.id) {
              this.allReadyLike = true;
            }
          }
        }
        if (collect) {
          console.log(collect, this.articles.id);
          if (collect.indexOf(this.articles.id) !== -1) {
            this.allReadycollect = true;
          }
        }
      },
    },
  },
  computed: {
    articles() {
      return this.$store.state.nowblog;
    },
    id() {
      return this.$store.state.nowblog.id;
    },
    context() {
      return this.articles.context;
    },
    tag() {
      return this.articles.tag;
    },
    code() {
      return this.articles.code;
    },
    real_them() {
      let k = this.$store.state.skin;
      let m = k == true ? "light" : "dark";
      return this.them + " " + m;
    },
    likeclass() {
      //点赞高亮
      let click = this.allReadyLike ? " click" : "";
      return `action-title-box${click}`;
    },
    collectclass() {
      //收藏高亮
      let clickcollect = this.allReadycollect ? " clickcollect" : "";
      return `action-title-box${clickcollect}`;
    },
    commentclass() {
      let click = this.ifReply ? " click" : "";
      return `action-title-box${click}`;
    },
    beforeid: {
      get() {
        return this.$store.state.nowblog.id;
      },
      set(v) {
        v;
      },
    },
  },
};
</script>

<style lang="less" scoped>
@import "../main.less";

.light {
  position: relative;
  height: 100%;
  width: 100%;
  overflow: auto;
  .article-title {
    color: @fs_light_contain;
    transition: all 1s;
  }
  .reply /deep/ .el-textarea__inner {
    background-color: @bg_light_contain;
    transition: all 1s;
  }
  .reply /deep/ .el-input__count {
    background-color: @bg_light_contain;
    transition: all 1s;
  }

  .el-input__count {
    background-color: @bg_light_contain;
    transition: all 1s;
  }

  .codes,
  .hljs {
    background-color: @bg_dark;
    transition: all 1s;
  }
}

.dark {
  position: relative;
  height: 100%;
  width: 100%;
  overflow: auto;

  .article-title {
    color: @fs_dark_contain;
    transition: all 1s;
  }

  .reply /deep/ .el-textarea__inner {
    background-color: @bg_dark;
    transition: all 1s;
  }
  .reply /deep/ .el-input__count {
    background-color: @bg_dark;
    transition: all 1s;
  }
  .el-input__count {
    background-color: @bg_dark;
    transition: all 1s;
  }

  .codes,
  .hljs {
    background-color: @bg_dark;
    transition: all 1s;
  }
}

.tt{
  text-indent: 2em;
  margin-top: 10px;
  padding: 0 10px;
  color:rgb(135, 135, 135);
}

.action-bar {
  position: absolute;
  z-index: 100;
  bottom: 0;
  width: 40%;
  right: 20px;
}

.action-bar {
  display: flex;
}

.codes {
  padding: 15px;
}

pre code.hljs {
  padding: 0;
}

.action {
  margin-right: 0%;
  justify-content: end;
  display: flex;
  align-items: center;
  width: 33%;
  position: relative;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.action-title-box {
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-title-box i {
  font-size: 20px;
}

.action-title-box .action-title {
  margin-left: 0.3em;
  font-size: 15px;
  font-weight: 500;
}

.article {
  padding-left: 2rem;
  padding-right: 2rem;
  position: relative;
  padding-top: 2rem;
  padding-bottom: 2rem;
  z-index: 1;
}

.headers {
  display: flex;
  align-items: center;
}

.tag {
  font-size: 1rem;
  display: flex;
  align-items: center;
}

.article-title {
  width: 80%;
  margin: 0 0 1.667rem;
  font-size: 2rem;
  line-height: 1.31;
}

.meta-box {
  font-size: 1rem;
  color: #8a919f;
  margin-top: 2px;
  line-height: 22px;
}

.author-info-block {
  display: flex;
  align-items: center;
}

.author-info-block .avatar {
  flex: 0 0 auto;
  margin-right: 1rem;
  width: 3.333rem;
  height: 3.333rem;
  border-radius: 50%;
}

.author-info-block .author-info-box {
  min-width: 0;
  flex-grow: 1;
  flex: 1;
  min-height: 43px;
}

.author-info-block .author-name {
  height: 2rem;
}

.author-info-block .author-name .username {
  width: 100%;
  font-size: 1rem;
  font-weight: 500;
  color: #515767;
  line-height: 2rem;
}

.username .name {
  display: inline-block;
  vertical-align: top;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meta-box {
  font-size: 1rem;
  color: #8a919f;
  margin-top: 2px;
  line-height: 22px;
}

.follow-button {
  margin: 0 0 0 auto;
  padding: 0 1rem;
  height: 34px;
  font-size: 14px;
  color: #1e80ff;
  background: rgba(30, 128, 255, 0.05);
  border: 1px solid rgba(30, 128, 255, 0.3);
  border-radius: 4px;
  display: flex;
  align-items: center;
}

.click {
  color: @fs_btn_color;
}
.clickcollect {
  color: #ffaa35;
}

.reply {
  width: 95%;
  margin: auto;
  padding-left: 30px;
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
  margin-top: 20px;
}

.el-textarea__inner {
  position: relative;
  display: inline-block;
  width: 100%;
  vertical-align: bottom;
  font-size: 14px;
}

.el-textarea .el-input__count {
  position: absolute;
  font-size: 12px;
  bottom: 5px;
  right: 10px;
}

.reply .btn {
  cursor: pointer;
  margin: 5px 0;
  height: 35px;
  width: 80px;
  border-radius: 4px;
  border: 1px solid #1e80ff;
  background-color: rgba(30, 128, 255, 0.05);
  color: #007fff;
  font-size: 14px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>